//
//  ForgetPWView.m
//  吃货·上海
//
//  Created by 濮亚军 on 15/11/28.
//  Copyright © 2015年 濮亚军. All rights reserved.
//

#import "ForgetPWView.h"
#import <BmobSDK/Bmob.h>
@interface ForgetPWView()<UITextFieldDelegate>

@property(nonatomic, strong)UIView *shadowView;

@end
@implementation ForgetPWView
{
    //判断选择状态
    IsSelected isSelected;
}
-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        isSelected = isBack;
        //阴影
        self.shadowView = [[UIView alloc]initWithFrame:frame];
        self.shadowView.backgroundColor = [UIColor blackColor];
        self.shadowView.alpha = 0.5;
        [self addSubview:_shadowView];
        
        //背景
        self.bgView = [[UIView alloc]initWithFrame:CGRectMake((frame.size.width - 200 * KWNum) / 2, (frame.size.height - 200 * KHNum) / 2, 200 * KWNum, 200 * KWNum)];
        self.bgView.backgroundColor = KColor;
        self.bgView.alpha = 1;
        [self addSubview:_bgView];
        
        //CGFloat x = (200 * KWNum - 50 * KWNum) / 2;
        //手机
        self.phoneImageView = [[UIImageView alloc]initWithFrame:CGRectMake((self.bgView.frame.size.width - 50 * KWNum) / 2, 10 * KWNum, 50 * KWNum, 30)];
        self.phoneImageView.image = [UIImage imageNamed:@"phone"];
        self.phoneImageView.userInteractionEnabled = YES;
        //self.phoneImageView.backgroundColor = [UIColor redColor];
        self.phoneImageView.layer.masksToBounds = YES;
        [self.phoneImageView.layer setCornerRadius:8];
        //[self.bgView addSubview:_phoneImageView];
        
        //手机手势方法
        UITapGestureRecognizer *phoneTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(phoneTapAction:)];
        [self.phoneImageView addGestureRecognizer:phoneTap];
        
        //邮箱
        self.emailImageView = [[UIImageView alloc]initWithFrame:CGRectMake((self.bgView.frame.size.width - 50 * KWNum) / 2, 10 * KWNum, 50 * KWNum, 30)];
        self.emailImageView.userInteractionEnabled = YES;
        //self.emailImageView.backgroundColor = [UIColor redColor];
        self.emailImageView.image = [UIImage imageNamed:@"email"];
        self.emailImageView.layer.masksToBounds = YES;
        [self.emailImageView.layer setCornerRadius:8];
        [self.bgView addSubview:_emailImageView];
        //邮箱手势方法
        UITapGestureRecognizer *emailTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(emailTapAction:)];
        [self.emailImageView addGestureRecognizer:emailTap];
        
        //输入
        self.userNameTF = [[UITextField alloc]initWithFrame:CGRectMake(40 * KWNum, 50 * KWNum, (200 * KWNum - 80 * KWNum), 25 * KWNum)];
        self.userNameTF.backgroundColor = [UIColor whiteColor];
        self.userNameTF.delegate = self;
        self.userNameTF.placeholder = @"用户名";
        self.userNameTF.font = [UIFont systemFontOfSize:14];
        self.userNameTF.layer.masksToBounds = YES;
        [self.userNameTF.layer setCornerRadius:8];
        self.userNameTF.userInteractionEnabled = NO;
        [self.bgView addSubview:_userNameTF];
        
        //输入
        self.inputTF = [[UITextField alloc]initWithFrame:CGRectMake(20 * KWNum, 80 * KWNum, (200 * KWNum - 40 * KWNum), 30 * KWNum)];
        self.inputTF.backgroundColor = [UIColor whiteColor];
        self.inputTF.delegate = self;
        self.inputTF.layer.masksToBounds = YES;
        [self.inputTF.layer setCornerRadius:8];
        self.inputTF.userInteractionEnabled = NO;
        [self.bgView addSubview:_inputTF];
        
        //确认
        self.cmImageView = [[UIImageView alloc]initWithFrame:CGRectMake(50 * KWNum, 110 * KWNum, (200 * KWNum - 100 * KWNum), 40)];
        self.cmImageView.image = [UIImage imageNamed:@"cmPhoneOrEmail"];
        //self.cmImageView.backgroundColor = [UIColor redColor];
        self.cmImageView.layer.masksToBounds = YES;
        [self.cmImageView.layer setCornerRadius:8];
        [self.bgView addSubview:_cmImageView];
        UITapGestureRecognizer *cmTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(cmTapAction:)];
        [self.cmImageView addGestureRecognizer:cmTap];
        
        //确认
        self.cancleImageView = [[UIImageView alloc]initWithFrame:CGRectMake(75 * KWNum, 160 * KWNum, (200 * KWNum - 150 * KWNum), 30)];
        self.cancleImageView.image = [UIImage imageNamed:@"cancelForgetPW"];
        self.cancleImageView.userInteractionEnabled = YES;
        //self.cancleImageView.backgroundColor = [UIColor redColor];
        self.cancleImageView.layer.masksToBounds = YES;
        [self.cancleImageView.layer setCornerRadius:8];
        [self.bgView addSubview:_cancleImageView];
        UITapGestureRecognizer *cancelTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(cancelTapAction:)];
        [self.cancleImageView addGestureRecognizer:cancelTap];
    }
    return self;
}
//手机手势方法
-(void)phoneTapAction:(UITapGestureRecognizer *)phoneTap
{
    NSLog(@"手机");
    isSelected = isPhone;
    
    [UIView animateWithDuration:0.5 animations:^{
        self.emailImageView.alpha = 0;
        self.phoneImageView.frame = CGRectMake( (self.bgView.frame.size.width - 50 * KWNum) / 2, 10 * KWNum, 50 * KWNum, 30);
        self.emailImageView.frame = CGRectMake( (self.bgView.frame.size.width - 50 * KWNum) / 2, 10 * KWNum, 50 * KWNum, 30);
        self.inputTF.placeholder = @"请输入手机号";
    } completion:^(BOOL finished) {
        self.inputTF.userInteractionEnabled = YES;
        self.userNameTF.userInteractionEnabled = YES;
        self.emailImageView.hidden = YES;
        self.cmImageView.userInteractionEnabled = YES;
        self.phoneImageView.userInteractionEnabled = NO;
        self.emailImageView.userInteractionEnabled = NO;
    }];
    
    
}
//邮箱手势方法
-(void)emailTapAction:(UITapGestureRecognizer *)emailTap
{
    NSLog(@"邮箱");
    isSelected = isEmail;
    [UIView animateWithDuration:0.5 animations:^{
        self.phoneImageView.alpha = 0;
        self.emailImageView.frame = CGRectMake( (self.bgView.frame.size.width - 50 * KWNum) / 2, 10 * KWNum, 50 * KWNum, 30);
        self.phoneImageView.frame = CGRectMake( (self.bgView.frame.size.width - 50 * KWNum) / 2, 10 * KWNum, 50 * KWNum, 30);
        self.inputTF.placeholder = @"请输入邮箱";
    } completion:^(BOOL finished) {
        self.inputTF.userInteractionEnabled = YES;
        self.userNameTF.userInteractionEnabled = YES;
        self.phoneImageView.hidden = YES;
        self.cmImageView.userInteractionEnabled = YES;
        self.phoneImageView.userInteractionEnabled = NO;
        self.emailImageView.userInteractionEnabled = NO;
    }];
}
//取消手势方法
-(void)cancelTapAction:(UITapGestureRecognizer *)cancelTap
{
    switch (isSelected)
    {
        case isPhone:
        {
            isSelected = isBack;
            NSLog(@"手机返回");
            [UIView animateWithDuration:0.5 animations:^{
                self.phoneImageView.frame = CGRectMake( (200 * KWNum - 140 * KWNum) / 2, 10 * KWNum, 50 * KWNum, 30);
                self.emailImageView.frame = CGRectMake( (200 * KWNum - 140 * KWNum) / 2 + 90 * KWNum, 10 * KWNum, 50 * KWNum, 30);
                self.emailImageView.hidden = NO;
                self.emailImageView.alpha = 1;
                self.cmImageView.userInteractionEnabled = NO;
                self.inputTF.userInteractionEnabled = NO;
                self.userNameTF.userInteractionEnabled = NO;
                self.inputTF.placeholder = NULL;
            } completion:^(BOOL finished) {
                self.emailImageView.userInteractionEnabled = YES;
                self.phoneImageView.userInteractionEnabled = YES;
            }];
            break;
        }
            
        case isEmail:
        {
            isSelected = isBack;
            NSLog(@"邮箱返回");
            [UIView animateWithDuration:0.5 animations:^{
                self.emailImageView.frame = CGRectMake( (self.bgView.frame.size.width - 50 * KWNum) / 2, 10 * KWNum, 50 * KWNum, 30);
                self.phoneImageView.frame = CGRectMake(  (200 * KWNum - 140 * KWNum) / 2, 10 * KWNum, 50 * KWNum, 30);
                self.phoneImageView.hidden = NO;
                self.phoneImageView.alpha = 1;
                self.cmImageView.userInteractionEnabled = NO;
                self.inputTF.userInteractionEnabled = NO;
                self.userNameTF.userInteractionEnabled = NO;
                self.inputTF.placeholder = NULL;
            } completion:^(BOOL finished) {
                self.emailImageView.userInteractionEnabled = YES;
                self.phoneImageView.userInteractionEnabled = YES;
            }];
            break;
        }
        case isBack:
        {
            isSelected = isBack;
            NSLog(@"返回");
            self.hidden = YES;
            [UIView animateWithDuration:0.5 animations:^{
                self.phoneImageView.frame = CGRectMake( (200 * KWNum - 140 * KWNum) / 2, 10 * KWNum, 50 * KWNum, 30);
                self.emailImageView.frame = CGRectMake( (200 * KWNum - 140 * KWNum) / 2 + 90 * KWNum, 10 * KWNum, 50 * KWNum, 30);
                self.emailImageView.hidden = NO;
                self.emailImageView.alpha = 1;
                self.cmImageView.userInteractionEnabled = NO;
                self.inputTF.userInteractionEnabled = NO;
                self.userNameTF.userInteractionEnabled = NO;
                self.inputTF.placeholder = NULL;
                self.userNameTF.text = NULL;
                self.inputTF.text = NULL;
            } completion:^(BOOL finished) {
                self.emailImageView.userInteractionEnabled = YES;
                self.phoneImageView.userInteractionEnabled = YES;
            }];
            break;
        }
        default:
            break;
    }
}
//获取手机方法
-(void)cmTapAction:(UITapGestureRecognizer *)cmTap
{
    switch (isSelected)
    {
        case isPhone:
        {
            NSLog(@"获取手机");
            isSelected = isBack;
            /*
            [BmobUser loginInbackgroundWithMobilePhoneNumber:self.inputTF andSMSCode:smsCode block:^(BmobUser *user, NSError *error) {
                if (user) {
                    NSLog(@"%@",user);
                } else {
                    NSLog(@"%@",error);
                }
            }];
             */
            BmobQuery *query = [BmobUser query];
            [query whereKey:@"username" equalTo:self.userNameTF.text];
            [query findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) {
                for (BmobUser *user in array)
                {
                    if ([user.mobilePhoneNumber isEqualToString:self.inputTF.text])
                    {
                        [BmobUser resetPasswordInbackgroundWithSMSCode:self.inputTF.text andNewPassword:self.inputTF.text block:^(BOOL isSuccessful, NSError *error) {
                            if (isSuccessful) {
                                NSLog(@"%@",@"重置密码成功");
                            } else {
                                NSLog(@"%@",error);
                            }
                        }];
                    }
                    else
                    {
                        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"邮箱不对不存在" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                        [alertView show];
                    }
                    
                }
            }];
            break;
        }
        case isEmail:
        {
            NSLog(@"获取邮箱");
            isSelected = isBack;
            BmobQuery *query = [BmobUser query];
            [query whereKey:@"username" equalTo:self.userNameTF.text];
            [query findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) {
                for (BmobUser *user in array)
                {
                    if ([user.email isEqualToString:self.inputTF.text])
                    {
                        [BmobUser requestPasswordResetInBackgroundWithEmail:self.inputTF.text];
                        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"重置密码成功" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
                        [alertView show];
                    }
                    else
                    {
                        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"邮箱不对不存在" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                        [alertView show];
                    }
                    
                }
            }];
            break;
        }
        default:
            break;
    }
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}










@end
